package com.lzy.system.organization.controller;

import com.lzy.conf.JSONResult;
import com.lzy.system.organization.dto.SysNavAddDTO;
import com.lzy.system.organization.dto.SysNavSortDTO;
import com.lzy.system.organization.dto.convert.SysNavUpdateDTO;
import com.lzy.system.organization.entity.security.entity.SecurityUser;
import com.lzy.system.organization.service.ISysNavService;
import com.lzy.utils.ValidList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 导航菜单 前端控制器
 * </p>
 *
 * @author lcc
 * @since 2023-12-05
 */
@RestController
@RequestMapping("/organization/sysNav")
@Api(tags = "导航菜单")
public class SysNavController {
    ISysNavService iSysNavService;

    @Autowired
    public SysNavController(ISysNavService iSysNavService) {
        this.iSysNavService = iSysNavService;
    }

    @ApiOperation("根据id查询导航")
    @RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
    public JSONResult getById(@ApiParam("导航id") @PathVariable(value = "id", required = true) long id) throws Exception {
        return JSONResult.ok(iSysNavService.getById(id));
    }

    @ApiOperation("查询导航列表")
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public JSONResult getList() throws Exception {
        return JSONResult.ok(iSysNavService.getList());
    }

    @ApiOperation("查询用户导航列表")
    @RequestMapping(value = "getListUserNav", method = RequestMethod.POST)
    public JSONResult getListUserNav() throws Exception {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SecurityUser securityUser = (SecurityUser)authentication.getPrincipal();
        return JSONResult.ok(iSysNavService.getListUserNav(securityUser.getUsername()));
    }

    @ApiOperation("添加导航")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JSONResult save(@ApiParam("导航信息") @RequestBody @Validated() SysNavAddDTO dto) throws Exception {
        return JSONResult.ok(iSysNavService.add(dto));
    }

    @ApiOperation("修改导航")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public JSONResult update(@ApiParam("导航信息") @RequestBody @Validated() SysNavUpdateDTO dto) throws Exception {
        return JSONResult.ok(iSysNavService.update(dto));
    }

    @ApiOperation("删除导航")
    @RequestMapping(value = "delById/{id}", method = RequestMethod.DELETE)
    public JSONResult delById(@ApiParam("导航id") @PathVariable(value = "id", required = true) long id) throws Exception {
        return JSONResult.ok(iSysNavService.delById(id));
    }

    @ApiOperation("批量删除导航")
    @RequestMapping(value = "delBatch", method = RequestMethod.DELETE)
    public JSONResult del(
            @ApiParam("导航列表") @RequestBody @Validated() ValidList<Integer> list
    ) throws Exception {
        return JSONResult.ok(iSysNavService.delBatch(list));
    }

    @ApiOperation("保存导航排序")
    @RequestMapping(value = "saveOrder", method = RequestMethod.POST)
    public JSONResult saveOrder(
            @ApiParam("导航列表") @RequestBody @Validated() ValidList<SysNavSortDTO> sysNavSortDTOS
    ) throws Exception {
        iSysNavService.saveOrder(sysNavSortDTOS);
        return JSONResult.ok();
    }
}
